package com.project.bookstore.cart.controller;

import com.project.bookstore.cart.entity.Cart;
import com.project.bookstore.cart.pojo.CartClientLocalVO;
import com.project.bookstore.cart.pojo.CartVO;
import com.project.bookstore.cart.service.ICartService;
import com.project.bookstore.common.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
@Api(tags = "购物车")
public class CartController {

    @Autowired
    private ICartService cartService;

    @PostMapping("/mergeLocalCart/{userId}")
    public CommonResult<Void> mergeLocalCart(@PathVariable Long userId,
                                             @RequestBody CartClientLocalVO[] carts){
        return cartService.mergeLocalCart(userId,carts);
    }

    @ApiOperation("未登录状态获取购物车列表")
    @PostMapping("/getCartList")
    public CommonResult<List<CartVO>> getByBookIds(@RequestBody CartClientLocalVO[] clientLocalVOS) {
        return CommonResult.ok(cartService.getCartByBookIds(clientLocalVOS));
    }

    @ApiOperation("获取购物车列表")
    @GetMapping("/getCartList/{userId}")
    public CommonResult<List<CartVO>> getCart(@PathVariable Long userId) {
        return CommonResult.ok(cartService.getUserCartList(userId));
    }

    @ApiOperation("根据ID删除购物车数据")
    @DeleteMapping("/deleteById/{id}")
    public CommonResult<Void> deleteById(@PathVariable Integer id){
        cartService.deleteCartById(id);
        return CommonResult.ok();

    }
    @ApiOperation("添加到购物车")
    @PostMapping("/add/{userId}")
    @ApiImplicitParams(
            @ApiImplicitParam(value = "用户id",name = "userId",example = "1",required = true)
    )
    public CommonResult<Void> add(@RequestBody CartClientLocalVO cartClientLocalVO,
                                  @PathVariable Long userId){
        return cartService.add(cartClientLocalVO.getBookId(),cartClientLocalVO.getCount(),userId);
    }
}
